package com.example.dell.ssm.model;

import com.example.dell.ssm.app.Api;
import com.example.dell.ssm.app.DoSchedule;
import com.example.dell.ssm.app.RetrofitManager;
import com.example.dell.ssm.contract.MainContract;
import com.example.dell.ssm.model.bean.ResponseBean;
import com.google.gson.Gson;
import com.jaydenxiao.common.commonutils.LogUtils;

import java.io.IOException;

import retrofit2.Call;
import retrofit2.Response;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * Created by dell on 2017/6/9.
 */

public class MainModel implements MainContract.Model {
    @Override
    public Observable<ResponseBean> register(final String name, final String password) {
        return Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                LogUtils.loge("=====--------------=========");
                Call<String> register = RetrofitManager.getInstance().createService(Api.class).register(name, password);
                Response<String> execute = null;
                try {
                    execute = register.execute();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String s = execute.body().toString();
                LogUtils.loge(s+"--------------=========");
                subscriber.onNext(s);
            }

        }).map(new Func1<String, ResponseBean>() {
            @Override
            public ResponseBean call(String s) {
                return new Gson().fromJson(s,ResponseBean.class);
            }
        }).compose(DoSchedule.<ResponseBean>applySchedulers());
    }
}
